package core;

import java.io.OutputStream;

import tools.Format;
import tools.Utils;

public class Message {
    private int type;
	private OutputStream os;
    private byte[] length = new byte[4];
    private byte[] id = new byte[1];
    private byte[] payload;

    public Message(int type, OutputStream os) {
        this.type = type;
        this.setData(type);
		this.os = os;
    }

    public Message(int type, byte[] payload,OutputStream os) {
        this.type = type;
        this.setData(type, payload);
		this.os = os;
    }

	public OutputStream getOutput() {	
		return this.os;	
	}


    public byte[] getLength() {
        return this.length;
    }

    public byte[] getID() {
        return this.id;
    }

    public byte[] getPayload() {
        return this.payload;
    }

    public void setLength(byte[] length) {
        this.length = length;
    }

    public void setID(int id) {
        this.id[0] = (byte) id;
    }

    public void setPayload(byte[] payload) {
        this.payload = payload;
    }

    public void setData(int type) {
        this.type = type;
        switch (type) {
        case 0:
            this.length = new byte[] {0, 0, 0, 1};
            this.id[0] = 0;
            break;
        case 1:
            this.length = new byte[] {0, 0, 0, 1};
            this.id[0] = 1;
            break;
        case 2:
            this.length = new byte[] {0, 0, 0, 1};
            this.id[0] = 2;
            break;
        case 3:
            this.length = new byte[] {0, 0, 0, 1};
            this.id[0] = 3;
            break;
        }
    }

    public void setData(int type, byte[] payload) {
        this.type = type;
        switch (type) {
        case 4:
            this.length = new byte[] {0, 0, 0, 5};
            this.id[0] = 4;
            this.payload = payload;
            break;
        case 5:
            this.length = Format.intToByteArray(1 + payload.length);
            this.id[0] = 5;
            this.payload = payload;
            break;
        case 6:
            this.length = new byte[] {0, 0, 0, 13};
            this.id[0] = 6;
            this.payload = payload;
            break;
        case 7:
            this.length = Format.intToByteArray(1 + payload.length);
            this.id[0] = 7;
            this.payload = payload;
            break;
        case 8:
            this.length = new byte[] {0, 0, 0, 13};
            this.id[0] = 8;
            this.payload = payload;
            break;
        case 9:
            this.length = new byte[] {0, 0, 0, 3};
            this.id[0] = 9;
            this.payload = payload;
            break;
        }
    }

    public void setData(byte[] length, byte id, byte[] payload) {
        this.length = length;
        this.id[0] = id;
        this.payload = payload;
    }

    public byte[] generate() {
        if (this.type > 4)
            return Utils.concat(Utils.concat(this.length, this.id),
                                this.payload);
        else if (this.type > 0)
            return Utils.concat(this.length, this.id);
        else
            return this.length;
    }

    
}

